<template>
  <!-- visible也可以设置一个sync修饰符 但是这里不能设置
   el-dialog 中的 visible.sync这个修饰符 是给el-dialog组件设置的 也就是 组件里 会进行一个事件触发
    this.$emit("update:visible", false)
    因为我们这里的visible后面的值是一个 props属性 props 是父组件的属性 是在子组件中只读的 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addEmployee" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" style="width:50%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" style="width:50%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" placeholder="请选择日期" style="width:50%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择" style="width:50%">
          <!-- 只能遍历组件的数据 -->
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="请输入工号" style="width:50%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="请输入选择部门" style="width:50%" @focus="getDepartments" />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          default-expand-all=""
          :props="{label:'name'}"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" placeholder="请选择日期" style="width:50%" />
      </el-form-item>
    </el-form>
    <!-- 底部摁钮 -->
    <el-row type="flex" justify="center" align="center">
      <el-col :span="6">
        <el-button type="primary" size="samll" @click="btnOK">确定</el-button>
        <el-button size="samll" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import { transListToTreeData } from '@/utils'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }, { min: 2, max: 8, message: '用户名为2-8位' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      },
      //   在data中定义数据
      EmployeeEnum,
      //   表单数据
      // 定义数组接收树形数据
      treeData: [],
      //   控制树形的显示和隐藏
      showTree: false,
      //   加载进度
      loading: false
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      //   depts是数组,不是树形
      this.treeData = transListToTreeData(depts, '')
      this.loading = false
    },
    // 点击部门触发事件
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    // 确定事件
    async btnOK() {
      try {
        // 校验整个表单
        await this.$refs.addEmployee.validate()
        await addEmployee(this.formData) // 新增员工请求
        this.$parent.getEmployeeList() // 告诉父组件更新
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 取消事件
    btnCancel() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addEmployee.resetFields() // 重置校验
      this.$parent.showDialog = false
    }
  }

}
</script>

<style>

</style>
